package fr.charlott.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.charlott.business.dto.CategorieDTO;
import fr.charlott.business.service.CategorieService;
import fr.charlott.core.repository.model.Categorie;

@Service("categorieService")
public class CategorieServiceImpl implements CategorieService {

	private fr.charlott.core.service.CategorieService categorieService;

	private DozerBeanMapper dozerMapper;

	public CategorieServiceImpl() {
		super();
	}

	@Autowired(required = true)
	public CategorieServiceImpl(fr.charlott.core.service.CategorieService categorieService, DozerBeanMapper dozerMapper) {
		this();
		this.categorieService = categorieService;
		this.dozerMapper = dozerMapper;
	}

	@Override
	@Transactional(readOnly = true)
	public CategorieDTO chargerParId(Long id) {
		Categorie categorie = categorieService.chargerParId(id);
		if (categorie != null){
			return dozerMapper.map(categorie, CategorieDTO.class);
		}
		return null;
	}
	
	@Override
	@Transactional(readOnly = true)
	public CategorieDTO chercherParNom(String nom) {
		Categorie categorie = categorieService.chercherParNom(nom);
		if (categorie != null){
			return dozerMapper.map(categorie, CategorieDTO.class);
		}
		return null;
	}
	
	@Override
	@Transactional(readOnly = true)
	public List<CategorieDTO> chargerToutesLesCategories() {
		List<CategorieDTO> listeCategories = new ArrayList<CategorieDTO>();
		for (Categorie categorie : categorieService.chargerToutesLesCategories()) {
			listeCategories.add(dozerMapper.map(categorie, CategorieDTO.class));
		}
		return listeCategories;
	}
	
	@Override
	@Transactional
	public void ajouterCategorie(CategorieDTO categorieDTO){
		categorieService.ajouterCategorie(dozerMapper.map(categorieDTO, Categorie.class));
	}
	
	@Override
	@Transactional
	public void importerCategorie(CategorieDTO categorieDTO) {
		if (categorieService.chercherParNom(categorieDTO.getNom()) == null){
			categorieService.ajouterCategorie(dozerMapper.map(categorieDTO, Categorie.class));
		}
	}
	
	@Override
	@Transactional
	public void supprimerCategorie(CategorieDTO categorieDTO) {
		Categorie categorie = dozerMapper.map(categorieDTO, Categorie.class);
		categorieService.supprimerCategorie(categorie);
	}
	
	@Override
	@Transactional
	public void supprimerToutesLesCategories() {
		categorieService.supprimerToutesLesCategories();
	}


	// getters - setters

	/**
	 * @param categorieMapper
	 *            the categorieMapper to set
	 */
	public void setDozerMapper(DozerBeanMapper dozerMapper) {
		this.dozerMapper = dozerMapper;
	}

	/**
	 * @param categorieService
	 *            the categorieService to set
	 */
	public void setCategorieService(fr.charlott.core.service.CategorieService categorieService) {
		this.categorieService = categorieService;
	}

}
